﻿Namespace EventAggregator

    Public Class EventPublisher
        Implements IEventAggregator

        Private m_listeners As New ArrayList


        Public Sub AddListener(listener As Object) Implements IEventAggregator.AddListener
            m_listeners.Add(listener)
        End Sub

        Public Sub PublishEvent(Of T)(subject As T) Implements IEventAggregator.PublishEvent
            For Each receiver As Object In From receiver1 In m_listeners Where Not IsNothing(DirectCast(receiver1, IListener(Of T)))
                DirectCast(receiver, IListener(Of T)).Handle(subject)
            Next
        End Sub

        Public Sub RemoveAll() Implements IEventAggregator.RemoveAll
            m_listeners.Clear()
        End Sub

        Public Sub RemoveListener(listener As Object) Implements IEventAggregator.RemoveListener
            m_listeners.Remove(listener)
        End Sub
    End Class

End Namespace